package jk.zmn.socket.tcp.demo02;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author 张梦楠
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2018/5/3118:46
 * @blog www.itzmn.com
 *
 *
 * 服务端
 */
public class ServerDemo2 {

     public static void main(String[] args) throws Exception{

         ServerSocket serverSocket = new ServerSocket(9999);

         //接收客户端连接，阻塞式
         Socket socket = serverSocket.accept();
         while (true) {
             //接收客户端传来的数据
             DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
             String s = dataInputStream.readUTF();
             System.out.println(s);

             //发送给客户端数据
             DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
             dataOutputStream.writeUTF("欢迎你"+s);
             dataOutputStream.flush();
         }
     }

}
